using System;

namespace ODX.Core
{
    /// <summary>
    /// This class is internally used to convert DataRow values to the .NET types.
    /// </summary>
    public static class Cast
    {
        public static DateTime ToDateTime(object value)
        {
            if (value == null || value is DBNull)
                return DateTime.MinValue;
            return Convert.ToDateTime(value);
        }

        public static Decimal ToDecimal(object value)
        {
            if (value == null || value is DBNull)
                return decimal.Zero;
            return Convert.ToDecimal(value);
        }

        public static Double ToDouble(object value)
        {
            if (value == null || value is DBNull)
                return 0;
            return Convert.ToDouble(value);
        }

        public static Single ToSingle(object value)
        {
            if (value == null || value is DBNull)
                return 0;
            return Convert.ToSingle(value);
        }

        public static Int64 ToInt64(object value)
        {
            if (value == null || value is DBNull)
                return 0;
            return Convert.ToInt64(value);
        }


        public static Int32 ToInt32(object value)
        {
            if (value == null || value is DBNull)
                return 0;
            return Convert.ToInt32(value);
        }

        public static Int16 ToInt16(object value)
        {
            if (value == null || value is DBNull)
                return 0;
            return Convert.ToInt16(value);
        }

        public static UInt64 ToUInt64(object value)
        {
            if (value == null || value is DBNull)
                return 0;
            return Convert.ToUInt64(value);
        }


        public static UInt32 ToUInt32(object value)
        {
            if (value == null || value is DBNull)
                return 0;
            return Convert.ToUInt32(value);
        }

        public static UInt16 ToUInt16(object value)
        {
            if (value == null || value is DBNull)
                return 0;
            return Convert.ToUInt16(value);
        }

        public static Byte ToByte(object value)
        {
            if (value == null || value is DBNull)
                return 0;
            return Convert.ToByte(value);
        }

        public static Boolean ToBoolean (object value)
        {
            if (value == null || value is DBNull)
                return false;
            return Convert.ToBoolean(value);
        }

        public static string ToString(object value)
        {
            if (value == null || value is DBNull)
                return string.Empty;
            return Convert.ToString(value);
        }

        public static byte[] ToByteArray(object value)
        {
            if (value == null || value is DBNull)
                return new byte[0];
            return (byte[])value;
        }

        public static Guid ToGuid(object value)
        {
            if (value == null || value is DBNull)
                return Guid.Empty;
            return new Guid(value.ToString());
        }

        public static string FromGuid(Guid value)
        {
            return value.ToString();
        }
    }
}
